package t10086.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import t10086.Repository.RobotRepository;
import t10086.Model.Entity.Robot;

import java.util.List;
import java.util.Optional;

@Service
public class RobotService {

    private final RobotRepository robotRepository;

    @Autowired  // 构造器注入
    public RobotService(RobotRepository robotRepository) {
        this.robotRepository = robotRepository;
    }

    public Optional<Robot> getRobotById(String id) {
        return robotRepository.findById(id);
    }
    public Optional<Robot> getRobotBySerialNumber(Long serialNumber) {
        return robotRepository.getRobotBySerialNumber(serialNumber);
    }

    public List<Robot> getAllRobots() {
        return robotRepository.findAll();
    }
    public List<Robot> getRobotsPaged(int pageNo, int pageSize) {
        return robotRepository.findAll(PageRequest.of(pageNo, pageSize)).getContent();
    }
    public boolean addRobot(Robot robot) {
           try {
                robotRepository.save(robot);//
           }catch (Exception e) {
                return false;
           }
        return true;
    }
}

